package com.cn.web.role.controller;

import com.cn.annoation.Logs;
import com.cn.msg.ResultMsg;
import com.cn.page.PageVo;
import com.cn.util.BaseController;
import com.cn.util.ExcelExportUtil;
import com.cn.web.role.service.RoleService;
import com.cn.web.role.vo.RoleEditDTO;
import com.cn.web.role.vo.RoleSearch;
import com.cn.web.role.vo.RoleVO;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * description: RoleController <br>
 * date: 21.12.16 13:45 <br>
 * author: cn_yaojin <br>
 * version: 1.0 <br>
 */
@RestController
@RequestMapping(value = "admin/role")
@Api(tags = "用户中心--角色管理")
public class RoleController extends BaseController {

    @Autowired
    private RoleService roleService;

    
    @Logs(value = "编辑", desc = "角色管理")
    @ApiOperation(value = "编辑")
    @PostMapping(value = "edit")
    public ResultMsg edit(
            @RequestBody @Validated RoleEditDTO roleEdit
    ) {
        this.roleService.edit(roleEdit);
        return ResultMsg.builder();
    }

    
    @ApiOperation(value = "分页")
    @PostMapping(value = "page")
    public ResultMsg<PageInfo<RoleVO>> page(
            @RequestBody PageVo<RoleSearch> pageVo
    ) {
        return ResultMsg.builder().setData(this.roleService.page(pageVo));
    }

    
    @ApiOperation(value = "查询启用中的所有角色")
    @GetMapping(value = "listRole")
    public ResultMsg<RoleVO> listRole() {
        RoleSearch roleSearch = new RoleSearch();
        roleSearch.setStatus("0");
        return ResultMsg.builder().setData(this.roleService.list(roleSearch));
    }

    
    @Logs(value = "删除", desc = "角色管理")
    @ApiOperation(value = "删除")
    @GetMapping(value = "del")
    public ResultMsg del(
            @ApiParam(name = "id") @RequestParam String id
    ) {
        this.roleService.del(id);
        return ResultMsg.builder();
    }

    
    @ApiOperation(value = "查看")
    @GetMapping(value = "find")
    public ResultMsg<RoleEditDTO> find(
            @ApiParam(name = "id") @RequestParam String id
    ) {
        return ResultMsg.builder().setData(this.roleService.find(id));
    }

    
    @Logs(value = "启用停用", desc = "角色管理")
    @ApiOperation(value = "启用停用")
    @GetMapping(value = "start")
    public ResultMsg start(
            @ApiParam(name = "id") @RequestParam Long id,
            @ApiParam(name = "status", value = "0-启用中 1-禁用中") @RequestParam String status
    ) {
        this.roleService.start(id, status);
        return ResultMsg.builder();
    }

    
    @ApiOperation(value = "导出")
    @PostMapping(value = "export")
    public void export(
            @RequestBody PageVo<RoleSearch> pageVo
    ) {
        var page = this.roleService.page(pageVo);
        if (page.getList() != null) {
            var list = page.getList();
            ExcelExportUtil.export1(response, "角色", "角色", RoleVO.class, list);
        }
    }
}
